﻿using System;
using System.Data;
using System.Collections;
using CE.Database;

/// <summary>
/// Summary description for CarPolicyDB
/// </summary>
namespace CE.CarInsurance
{
    public class CarPolicyDB
    {
        public CarPolicyDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarPolicy carpolicy)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocCarPolicyInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@CECarPolicyId", carpolicy.CECarPolicyId);
            _sPara.Add("@CECarId", carpolicy.CECarId);
            _sPara.Add("@TypeOfCoverage", carpolicy.TypeOfCoverage);
            _sPara.Add("@CoverageCommenceDate", carpolicy.CoverageCommenceDate);
            _sPara.Add("@NCDPercent", carpolicy.NCDPercent);
            _sPara.Add("@IsClaimLastThreeYear", carpolicy.IsClaimLastThreeYear);
            _sPara.Add("@IsInsCOE", carpolicy.IsInsCOE);
            _sPara.Add("@IsDriveMyOrTh", carpolicy.IsDriveMyOrTh);
            _sPara.Add("@ExcessPreference", carpolicy.ExcessPreference);
            _sPara.Add("@IsOffDriveCom", carpolicy.IsOffDriveCom);
            _sPara.Add("@TPDP", carpolicy.TPDP);
            _sPara.Add("@IsNCDProtection", carpolicy.IsNCDProtection);
            _sPara.Add("@IsChangeVE", carpolicy.IsChangeVE);
            _sPara.Add("@IncDesExcess", carpolicy.IncDesExcess);
            _sPara.Add("@IsMainDriver", carpolicy.IsMainDriver);
            _sPara.Add("@IsAddDriver", carpolicy.IsAddDriver);
            _sPara.Add("@NoOfDriver", carpolicy.NoOfDriver);
            _sPara.Add("@RefCEUserId", carpolicy.RefCEUserId);
            _sPara.Add("@CurrentStep", carpolicy.CurrentStep);
            _sPara.Add("@ViewedDetailId", carpolicy.ViewedDetailId);
            _sPara.Add("@Status", carpolicy.Status);
            _sPara.Add("@Info1", carpolicy.Info1);
            _sPara.Add("@Info2", carpolicy.Info2);
            _sPara.Add("@Info3", carpolicy.Info3);
            _sPara.Add("@Info4", carpolicy.Info4);
            _sPara.Add("@Info5", carpolicy.Info5);
            _sPara.Add("@Info6", carpolicy.Info6);
            _sPara.Add("@Info7", carpolicy.Info7);
            _sPara.Add("@Info8", carpolicy.Info8);
            _sPara.Add("@Info9", carpolicy.Info9);
            _sPara.Add("@Info10", carpolicy.Info10);
            _sPara.Add("@Info11", carpolicy.Info11);
            _sPara.Add("@Info12", carpolicy.Info12);
            _sPara.Add("@Info13", carpolicy.Info13);
            _sPara.Add("@Info14", carpolicy.Info14);
            _sPara.Add("@Info15", carpolicy.Info15);
            _sPara.Add("@Info16", carpolicy.Info16);
            _sPara.Add("@Info17", carpolicy.Info17);
            _sPara.Add("@Info18", carpolicy.Info18);
            _sPara.Add("@Info19", carpolicy.Info19);
            _sPara.Add("@Info20", carpolicy.Info20);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public bool Update()
        {
            return true;
        }

        public bool Delete()
        {
            return true;
        }

        public CarPolicy GetCarPolicy(Int64 CarId, Int64 PolicyId, Int64 UserId)
        {
            CarPolicy _carpolicy = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCarPolicySelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@CECarID", CarId);
            _spara.Add("@CECarPolicyId", PolicyId);
            _spara.Add("@RefCEUserId", UserId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carpolicy = new CarPolicy();
                    _carpolicy = FillDataRecord(_dt);
                }
            }
            return _carpolicy;
        }

        public CarPolicy GetCarPolicy(Int64 CarId, Int64 UserId)
        {
            CarPolicy _carpolicy = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocCarPolicySelectItem]";

            SortedList _spara = new SortedList();
            _spara.Add("@CECarID", CarId);
            _spara.Add("@RefCEUserId", UserId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carpolicy = new CarPolicy();
                    _carpolicy = FillDataRecord(_dt);
                }
            }
            return _carpolicy;
        }

        private CarPolicy FillDataRecord(DataTable myDataTable)
        {
            CarPolicy myPolicy = new CarPolicy();
            myPolicy.CECarPolicyId = Convert.ToInt64(myDataTable.Rows[0]["CECarPolicyID"].ToString());
            myPolicy.CECarId = Convert.ToInt32(myDataTable.Rows[0]["CECarId"].ToString());
            myPolicy.TypeOfCoverage = myDataTable.Rows[0]["TypeOfCoverage"].ToString();
            myPolicy.CoverageCommenceDate = Convert.ToDateTime(myDataTable.Rows[0]["CoverageCommenceDate"].ToString());
            myPolicy.NCDPercent = Convert.ToSingle(myDataTable.Rows[0]["NCDPercent"].ToString());
            myPolicy.IsClaimLastThreeYear = Convert.ToBoolean(myDataTable.Rows[0]["IsClaimLastThreeYear"].ToString());
            myPolicy.IsOffDriveCom = Convert.ToBoolean(myDataTable.Rows[0]["IsOffDriveCom"].ToString());
            myPolicy.IsInsCOE = Convert.ToBoolean(myDataTable.Rows[0]["IsInsCOE"].ToString());
            myPolicy.IsDriveMyOrTh = Convert.ToBoolean(myDataTable.Rows[0]["IsDriveMyOrTh"].ToString());
            myPolicy.ExcessPreference = myDataTable.Rows[0]["ExcessPreference"].ToString();
            myPolicy.TPDP = myDataTable.Rows[0]["TPDP"].ToString();
            myPolicy.IsNCDProtection = Convert.ToBoolean(myDataTable.Rows[0]["IsNCDProtection"].ToString());
            myPolicy.IsChangeVE = Convert.ToBoolean(myDataTable.Rows[0]["IsChangeVE"].ToString());
            myPolicy.IncDesExcess = Convert.ToInt32(myDataTable.Rows[0]["IncDesExcess"].ToString());
            myPolicy.NoOfDriver = Convert.ToInt16(myDataTable.Rows[0]["NoOfDriver"].ToString());
            myPolicy.RefCEUserId = Convert.ToInt64(myDataTable.Rows[0]["RefCEUserId"].ToString());
            myPolicy.IsMainDriver = Convert.ToBoolean(myDataTable.Rows[0]["IsMainDriver"].ToString());
            myPolicy.IsAddDriver = Convert.ToBoolean(myDataTable.Rows[0]["IsAddDriver"].ToString());
            myPolicy.ModifiedDate = Convert.ToDateTime(myDataTable.Rows[0]["ModifiedDate"].ToString());
            myPolicy.Info1 = myDataTable.Rows[0]["Info1"].ToString();
            myPolicy.Info2 = myDataTable.Rows[0]["Info2"].ToString();
            myPolicy.Info3 = myDataTable.Rows[0]["Info3"].ToString();
            myPolicy.Info4 = myDataTable.Rows[0]["Info4"].ToString();
            myPolicy.Info5 = myDataTable.Rows[0]["Info5"].ToString();
            myPolicy.Status = myDataTable.Rows[0]["Status"].ToString();
            return myPolicy;
        }

    }
}
